﻿using BCCommon;
using BCEntity.TMS.Agent.AgentNightFreightConfiguration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data;

namespace BCData.TMS.Agent.AgentNightFreightConfiguration
{
    public class AgentNightFreightConfigurationData : IAgentNightFreightConfigurationData
    {
        private readonly IDatabaseContext database;

        private const string QUERY = @"SELECT AgentNightFreightConfigurationId,AgentRecordId,NightFreightConfigurationId,OrderExtractProportion,StartHour,StartMinute,EndHour,EndMinute,CreateTime,CreateAdminId,CreateAdminName,UpdateTime,EditAdminId,EditAdminName  
                                    FROM AgentNightFreightConfiguration";

        public AgentNightFreightConfigurationData(IDatabaseContext database)
        {
            this.database = database;
        }

        public async Task<AgentNightFreightConfigurationEntity> Insert(AgentNightFreightConfigurationEntity entity)
        {
            return await database.Master.InsertAsync(entity);
        }

        public async Task<(IEnumerable<AgentNightFreightConfigurationEntity>, int)> Query(long agentRecordId, int? orderExtractProportion, int? startHour, int? startMinute, int? endHour, int? endMinute, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            wheres.Add("AgentRecordId = ?AgentRecordId");
            parameters.Add("?AgentRecordId", agentRecordId, DbType.Int64);
            if (orderExtractProportion.HasValue)
            {
                wheres.Add("OrderExtractProportion = ?OrderExtractProportion");
                parameters.Add("?OrderExtractProportion", orderExtractProportion.Value, DbType.Int32);
            }
            if (startHour.HasValue)
            {
                wheres.Add("StartHour >= ?StartHour");
                parameters.Add("?StartHour", startHour.Value, DbType.Int32);
            }
            if (startMinute.HasValue)
            {
                wheres.Add("StartMinute >= ?StartMinute");
                parameters.Add("?StartMinute", startMinute.Value, DbType.Int32);
            }
            if (endHour.HasValue)
            {
                wheres.Add("EndHour <= ?EndHour");
                parameters.Add("?EndHour", endHour.Value, DbType.Int32);
            }
            if (endMinute.HasValue)
            {
                wheres.Add("EndMinute <= ?EndMinute");
                parameters.Add("?EndMinute", endMinute.Value, DbType.Int32);
            }
            string sql = string.Format(@"{0} {1} LIMIT {2},{3}; 
                                SELECT COUNT(1)   
                                FROM AgentNightFreightConfiguration {1}",
                QUERY,
                wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : string.Empty,
                (pageIndex - 1) * pageSize,
                pageSize);
            return await database.Slave.QueryPageableListAsync<AgentNightFreightConfigurationEntity>(sql, parameters);
        }

        public async Task<AgentNightFreightConfigurationEntity> GetEntity(long agentNightFreightConfigurationId)
        {
            return await database.Slave.QueryEntityAsync<AgentNightFreightConfigurationEntity>($"{QUERY} WHERE AgentNightFreightConfigurationId = ?AgentNightFreightConfigurationId", new { agentNightFreightConfigurationId });
        }

        public async Task<AgentNightFreightConfigurationEntity> Update(AgentNightFreightConfigurationEntity entity)
        {
            return await database.Master.UpdateAsync(entity);
        }

        public async Task<int> Delete(long agentNightFreightConfigurationId)
        {
            return await database.Master.ExecuteAsync("DELETE FROM AgentNightFreightConfiguration WHERE AgentNightFreightConfigurationId = ?AgentNightFreightConfigurationId", new { agentNightFreightConfigurationId });
        }

        public async Task<AgentNightFreightConfigurationEntity> GetById(long agentRecordId, long nightFreightConfigurationId)
        {
            return await database.Slave.QueryEntityAsync<AgentNightFreightConfigurationEntity>($"{QUERY} WHERE AgentRecordId = ?AgentRecordId AND NightFreightConfigurationId = ?NightFreightConfigurationId", new { agentRecordId, nightFreightConfigurationId });
        }

        public async Task<IEnumerable<AgentNightFreightConfigurationEntity>> GetList(long agentRecordId, int? orderExtractProportion, int? startHour, int? startMinute, int? endHour, int? endMinute)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            wheres.Add("AgentRecordId = ?AgentRecordId");
            parameters.Add("?AgentRecordId", agentRecordId, DbType.Int64);
            if (orderExtractProportion.HasValue)
            {
                wheres.Add("OrderExtractProportion = ?OrderExtractProportion");
                parameters.Add("?OrderExtractProportion", orderExtractProportion.Value, DbType.Int32);
            }
            if (startHour.HasValue)
            {
                wheres.Add("StartHour >= ?StartHour");
                parameters.Add("?StartHour", startHour.Value, DbType.Int32);
            }
            if (startMinute.HasValue)
            {
                wheres.Add("StartMinute >= ?StartMinute");
                parameters.Add("?StartMinute", startMinute.Value, DbType.Int32);
            }
            if (endHour.HasValue)
            {
                wheres.Add("EndHour <= ?EndHour");
                parameters.Add("?EndHour", endHour.Value, DbType.Int32);
            }
            if (endMinute.HasValue)
            {
                wheres.Add("EndMinute <= ?EndMinute");
                parameters.Add("?EndMinute", endMinute.Value, DbType.Int32);
            }
            string sql = string.Format("{0} {1}",
                QUERY,
                wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : string.Empty);
            return await database.Slave.QueryAsync<AgentNightFreightConfigurationEntity>(sql, parameters);
        }
    }
}
